package top.wilbert.collegelink.forum.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.wilbert.collegelink.common.utils.PageUtils;
import top.wilbert.collegelink.common.utils.Query;

import top.wilbert.collegelink.common.vo.UserResponseVo;
import top.wilbert.collegelink.common.vo.UserSimpleInfoVo;
import top.wilbert.collegelink.forum.dao.TalkDao;
import top.wilbert.collegelink.forum.dao.ThumbDao;
import top.wilbert.collegelink.forum.entity.ThumbEntity;
import top.wilbert.collegelink.forum.feign.UserFeignService;
import top.wilbert.collegelink.forum.service.ThumbService;
import top.wilbert.collegelink.forum.vo.TalkResponseVo;


@Service("thumbService")
public class ThumbServiceImpl extends ServiceImpl<ThumbDao, ThumbEntity> implements ThumbService {

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    private TalkDao talkDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ThumbEntity> page = this.page(
                new Query<ThumbEntity>().getPage(params),
                new QueryWrapper<ThumbEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils listTalkInfo(IPage<TalkResponseVo> page, ArrayList<Long> targetIds) {
        List<TalkResponseVo> talkResponseVos = talkDao.selectTalkListByTalkIds(page, targetIds);
        List<Long> userIds = new ArrayList<>();
        for (TalkResponseVo talkResponseVo : talkResponseVos) {
            userIds.add(talkResponseVo.getCreatorId());
            talkResponseVo.setIsLike(true);
        }

        Map<Long, UserSimpleInfoVo> map = userFeignService.searchUserByIds(userIds);

        for (TalkResponseVo talkResponseVo : talkResponseVos) {
            talkResponseVo.setUserSimpleInfoVo(map.get(talkResponseVo.getCreatorId()));
        }

        IPage<TalkResponseVo> iPage = page.setRecords(talkResponseVos);

        return new PageUtils(iPage);
    }

    @Override
    public Boolean isLike(Long targetId, Integer targetType, Long userId) {
        Integer count = baseMapper.selectCount(new QueryWrapper<ThumbEntity>().eq("target_id", targetId).eq("target_type", targetType).eq("user_id", userId).eq("type", 0));
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean isCollect(Long targetId, Integer targetType, Long userId) {
        Integer count = baseMapper.selectCount(new QueryWrapper<ThumbEntity>().eq("target_id", targetId).eq("target_type", targetType).eq("user_id", userId).eq("type", 1));
        if (count > 0) {
            return true;
        }
        return false;
    }

}